import { ShellIntegrationService } from './../../../services/shellIntegration';
import { Employee } from './../../../models/Employee';
import { Observable, Subject, BehaviorSubject } from 'rxjs';
import { employeesService } from './../../../services/employee';
import { Component, OnInit, ElementRef } from '@angular/core';
import { BaseComponent } from "../../../components/baseComponent";
import * as R from "ramda";

@Component({
    template: `
        <div class="bracelet-container" fxLayout="row" fxFlex="100%">
           <bracelet-setup *ngIf="!(currentEmployee$ | async)" (select)="currentEmployee$.next($event)" [employees]="employees$ | async"></bracelet-setup>
           <bracelet-binding *ngIf="currentEmployee$ | async" [employee]="currentEmployee$ | async" [employees]="employees$ | async" (changeEmployee)="currentEmployee$.next($event)"></bracelet-binding>
        </div>
    `,
    styleUrls: ['./bracelet.scss']
})
export class BraceletComponent extends BaseComponent implements OnInit{
    constructor(    
        private shellIntegrationService:ShellIntegrationService,
        private element: ElementRef
    ){
        super()
    }

    employees$
    currentEmployee$
    async ngOnInit(){
        const initEmployees$ =  Observable.fromPromise(employeesService.getDishesDistributor());

        const currentEmployee$ = new BehaviorSubject<Employee | null> (null);
        this.currentEmployee$ = currentEmployee$;

        const employees$ = Observable.combineLatest(initEmployees$, currentEmployee$).map(([employees, currentEmployee])=>{
            employees.find(x=>x.id==(currentEmployee&&currentEmployee.id))&&(employees.find(x=>x.id==currentEmployee.id).extended = currentEmployee.extended)
            return employees;
        })
        this.employees$ =  employees$; 
    }
}
